<html>
  <head>
    <title>组件props简写</title>
  </head>
  <body>
    <div id="root"></div>
    <script type="text/javascript" src="../../js/react.development.js"></script>
    <script type="text/javascript" src="../../js/react-dom.development.js"></script>
    <script type="text/javascript" src="../../js/babel.min.js"></script>
    <script type="text/javascript" src="../../js/prop-types.js"></script>
    <script type="text/babel">
      class Person extends React.Component {
        static propTypes = {
          name: PropTypes.string.isRequired,
          age: PropTypes.number,
          sex: PropTypes.oneOf(['男', '女']),
          speak: PropTypes.func,
        }

        static defaultProps = {
          age: 0,
          sex: '不男不女',
          speak: () => {},
        };

        render() {
          const { name, age, sex, speak } = this.props;
          return (
            <ul onClick={() => speak(name)}>
              <li>姓名：{name}</li>
              <li>年龄：{age}</li>
              <li>性别：{sex}</li>
            </ul>
          );
        }
      }

      class Main extends React.Component {
        render() {
          const speak = (name) => {
            console.log(`Hello, My name is ${name}`);
          };

          const persons = [
            { name: 'Judith', age: 18, sex: '女', speak },
            { name: 'ZhangSan', sex: '男', speak },
          ];

          return (
            <>
              {
                persons.map((person, index) => <Person {...person} key={index} />)
              }
            </>
          );
        }
      }
      ReactDOM.render(<Main />, document.getElementById('root'));
    </script>
  </body>
</html>
